package com.huawei.demo;

import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.das.v3.DasClient;
import com.huaweicloud.sdk.das.v3.model.DeleteDbUserRequest;
import com.huaweicloud.sdk.das.v3.model.DeleteDbUserResponse;
import com.huaweicloud.sdk.das.v3.model.RegisterDbUserRequest;
import com.huaweicloud.sdk.das.v3.model.RegisterDbUserRequestBody;
import com.huaweicloud.sdk.das.v3.model.RegisterDbUserResponse;
import com.huaweicloud.sdk.das.v3.model.UpdateDbUserRequest;
import com.huaweicloud.sdk.das.v3.model.UpdateDbUserRequestBody;
import com.huaweicloud.sdk.das.v3.model.UpdateDbUserResponse;
import com.huaweicloud.sdk.das.v3.region.DasRegion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataBaseUserDemo {
    private static final Logger logger = LoggerFactory.getLogger(DataBaseUserDemo.class.getName());

    /**
     * args[0] = <<YOUR AK>>
     * args[1] = <<YOUR SK>>
     * args[2] = <<YOUR REGION_ID>>
     * args[3] = <<YOUR INSTANCE_ID>>
     * args[4] = <<YOUR USER_NAME>>
     * args[5] = <<YOUR PASSWORD>>
     * args[6] = <<YOUR DATASTORE_TYPE>>
     * args[7] = <<YOUR USER_ID>>
     * args[8] = <<YOUR NEW_USER_NAME>>
     * args[9] = <<YOUR NEW_PASSWORD>>
     */
    public static void main(String[] args) {
        String ak = args[0];
        String sk = args[1];
        String regionId = args[2];
        String instanceId = args[3];
        String username = args[4];
        String password = args[5];
        String datastoreType = args[6];
        String userId = args[7];
        String newUsername = args[8];
        String newPassword = args[9];

        ICredential auth = new BasicCredentials()
                .withAk(ak)
                .withSk(sk);
        DasClient client = DasClient.newBuilder()
                .withCredential(auth)
                .withRegion(DasRegion.valueOf(regionId))
                .build();

        // 注册数据库用户
        registerDbUser(client, instanceId, username, password, datastoreType);

        // 修改数据库用户
        updateDbUser(client, instanceId, userId, newUsername, newPassword);

        // 删除数据库用户
        deleteDbUser(client, instanceId, userId);
    }

    private static void registerDbUser(DasClient client, String instanceId, String username, String password,
            String datastoreType) {
        RegisterDbUserRequest request = new RegisterDbUserRequest();
        RegisterDbUserRequestBody requestBody = new RegisterDbUserRequestBody()
                .withDbUsername(username)
                .withDbUserPassword(password)
                .withDatastoreType(datastoreType);
        request.withInstanceId(instanceId);
        request.withBody(requestBody);

        invokeClient(() -> {
            RegisterDbUserResponse response = client.registerDbUser(request);
            logger.info(response.toString());
        });
    }

    private static void updateDbUser(DasClient client, String instanceId, String userId, String newUsername,
            String newPassword) {
        UpdateDbUserRequest request = new UpdateDbUserRequest();
        UpdateDbUserRequestBody requestBody = new UpdateDbUserRequestBody()
                .withDbUsername(newUsername)
                .withDbUserPassword(newPassword);
        request.withInstanceId(instanceId);
        request.withDbUserId(userId);
        request.withBody(requestBody);

        invokeClient(() -> {
            UpdateDbUserResponse response = client.updateDbUser(request);
            logger.info(response.toString());
        });
    }

    private static void deleteDbUser(DasClient client, String instanceId, String userId) {
        DeleteDbUserRequest request = new DeleteDbUserRequest();
        request.withInstanceId(instanceId);
        request.withDbUserId(userId);

        invokeClient(() -> {
            DeleteDbUserResponse response = client.deleteDbUser(request);
            logger.info(response.toString());
        });
    }

    private static <T> void invokeClient(Runnable runnable) {
        try {
            runnable.run();
        } catch (ConnectionException e) {
            logger.error("ConnectionException", e);
        } catch (RequestTimeoutException e) {
            logger.error("RequestTimeoutException", e);
        } catch (ServiceResponseException e) {
            logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(),
                    e.getErrorMsg());
        }
    }
}
